//
//  AppDelegate.m
//  ZPrograme
//
//  Created by 周成龙 on 16/6/16.
//  Copyright © 2016年 none. All rights reserved.
//

#import "AppDelegate.h"
#import "AppDelegate+UMeng.h"
#import "AppDelegate+BaiduMap.h"
#import "AppDelegate+EaseMob.h"

#import "TabBarVC.h"
#import "LoginVC.h"
#import "LaunchVC.h"

#import "UserModel.h"

#import "MarkHeader.h"
#import "VDJConstant.h"
#import "ZUserDefault.h"

#import "UMessage.h"
#import "SDWebImageManager.h"
#import "ZAlertControlTool.h"
#import <AlipaySDK/AlipaySDK.h>


#import "ViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    
    
    
    // 添加 app key
    // 百度地图
    [self setBaiduMapKitAppKey];
    // UMeng Share
    [self setUMengShareAppkey];
    // UMeng Message
//    [self pushMessagedidFinishLaunchingWithOptions:launchOptions];
    // EaseMob
    [self easemobApplication:application didFinishLaunchingWithOptions:launchOptions];
    
    
    
    
    /*
     第一次登录：是否查看引导页。
     1.是：判断是否登录。
     (1).登录：则进入app，(2).未登录：去登录
     
     2.否：去查看引导页。然后登录
     */
    NSString *launchStr = [ZUserDefault getStorageObjectWithKey:kLaunchSymbol];
    if (kStringAIsEqualToStringB(launchStr, kSymbol)) {
        
        if ([UserModel queryModel]) {
            [self login];
        }else {
            [self notLogin];
        }
    }else {
        
        [self gotoLaunch];
    }
    
    
    
    return YES;
}



#pragma mark - login
// 去加载 launch
- (void)gotoLaunch
{
    LaunchVC *vc = [[LaunchVC alloc] init];
    self.window.rootViewController = vc;
}

// 未登录
- (void)notLogin
{
    LoginVC *vc = [[LoginVC alloc] init];
    self.window.rootViewController = vc;
}

// 登录
- (void)login
{
    TabBarVC *viewController = [[TabBarVC alloc] init];
    self.window.rootViewController = viewController;
}

#pragma mark - alipay
- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {
    
    if ([url.host isEqualToString:@"safepay"]) {
        //跳转支付宝钱包进行支付，处理支付结果
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            MyLog(@"result = %@",resultDic);
        }];
    }
    return YES;
}

// NOTE: 9.0以后使用新API接口
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
{
    if ([url.host isEqualToString:@"safepay"]) {
        //跳转支付宝钱包进行支付，处理支付结果
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            MyLog(@"result = %@",resultDic);
        }];
    }
    return YES;
}

#pragma mark - SDWebImage
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
    // 停止所有的下载
    [[SDWebImageManager sharedManager] cancelAll];
    // 删除缓存
    [[SDWebImageManager sharedManager].imageCache clearMemory];
}


#pragma mark - private
- (void)removeUserInfo
{
    [[EMClient sharedClient] asyncRegisterWithUsername:@"17756234759" password:@"111111" success:^{
    } failure:^(EMError *aError) {
    }];
    [self removeAlias];
    [UserModel deleteAllFile];
}

-(void)removeAlias
{
//    UserModel * model = [UserModel queryModel];
//    NSString *alias = [NSString stringWithFormat:@"%@magicTeacher",model.uid];
//    NSString * psdMd5 = [PublicMethod md5:alias];
//    [UMessage removeAlias:psdMd5 type:kUMessageAlasType response:^(id responseObject, NSError *error) {
//
//    }];
}

@end
